package com.yitao.cms.utils;

import com.skipper.base.json.JSON;
import com.skipper.base.json.JSONObject;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * 快递100相关工具类
 *
 * @Date: 2019/6/11 20:10
 * @Author: Kien
 */
@Component
public class Kuaidi100CmsUtils {
    private static final String CUSTOMER = "544B760F901B9021E0EB904CC15E6604";

    private static final String KEY = "kWCbJSyp5378";

    private static final String URL = "https://poll.kuaidi100.com/poll/query.do"; // 实时查询地址

    private static final String SUBSCRIBE_URL = "http://poll.kuaidi100.com/poll"; // 消息订阅地址

    private static Logger logger = LoggerFactory.getLogger(Kuaidi100CmsUtils.class);

    /**
     *
     * 功能描述: 快递100订阅推送
     *
     * @param companyCode 快递公司编码
     * @param deliveryNo 快递单号
     * @param  callbackurl 回调地址
     * @return:
     * @auther: Kien
     * @date: 2019/4/28 17:32
     */
    public static String subscriptionKuaidi100(String companyCode, String deliveryNo, String callbackurl, String phone){
        if(StringUtils.isBlank(companyCode) || StringUtils.isBlank(deliveryNo)) {
            return null;
        }
        JSONObject param = new JSONObject();
        param.put("company", companyCode); // 快递公司编码
        param.put("number", deliveryNo); // 快递单号
        param.put("key", KEY);
        JSONObject parameters = new JSONObject();
        parameters.put("callbackurl", callbackurl); // 回调地址，必填
        if(StringUtils.isNotBlank(phone) && "null".equals(phone)) {
            parameters.put("phone", phone.trim()); // 如果是顺丰快递，必填
        }
        param.put("parameters", parameters);
        Map<String, String> params  = new HashMap<>();
        params.put("schema", "json");
        params.put("param", param.toJSONString());
        logger.info("http请求调用快递100接口");
        String result = HttpUtil.post(SUBSCRIBE_URL, params);
        return result;
    }
}
